

package com.c2soft.ncmxtradeX.controller;

import java.net.URI;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UriComponentsBuilder;

import com.c2soft.ncmxtradeX.command.BrokeOrderCommand;


@Log4j2
@Controller
@RequestMapping("/broker")
public class BrokeOrderMgmtController {
	
	@Value("#{systemProperties['ncmxServer.redirect.url']}")
	private String baseServerUrl;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping(value = "/show")
	public String show() {
		return "testOrder";
	}
	
	
	@RequestMapping(value = "/buyOrder", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public void buy(@RequestBody BrokeOrderCommand buy,HttpServletRequest req) {
		String redirectUrl = baseServerUrl+"broker/buy";
		restTemplate.postForObject(redirectUrl, buy, BrokeOrderCommand.class);
		
	}
	
	@RequestMapping(value = "/sellOrder", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public void sell(@RequestBody BrokeOrderCommand sell,HttpServletRequest req) {
		String redirectUrl =  baseServerUrl+"broker/sell";
		restTemplate.postForObject(redirectUrl, sell, BrokeOrderCommand.class);
		
	}
	
	@RequestMapping(value = "/userBuyDetail",method=RequestMethod.GET)
	public @ResponseBody List userBuyDetail(HttpServletRequest req) {
	
		String userId=req.getParameter("userName");
		
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("order/buyorderMgmt")
		    .queryParam("userId", userId)
		    .build()
		    .toUri();
	
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	
	String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
	return response;
}
	@RequestMapping(value = "/brokerBuyDetail",method=RequestMethod.GET)
	public @ResponseBody List brokerBuy(HttpServletRequest req) {
		String brokerId=req.getParameter("brokerName");
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("broker/buyorderMgmt")
		    .queryParam("brokerId", brokerId)
		    .build()
		    .toUri();
	
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	
	String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
	return response;
}
	@RequestMapping(value = "/exchangeBuyDetail",method=RequestMethod.GET)
	public @ResponseBody List exchangeBuy(HttpServletRequest req) {
	
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("broker/buyorderMgmtX")
		    .build()
		    .toUri();
	
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	
	String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
	return response;
}
	
	


}
